<?php

class TrxVoucherController extends Controller
{
	public function actionCetak()
	{
		$nomor = $_REQUEST['nomor'];
		$sql = " SELECT a.*,b.name FROM TRX_VOUCHER a left join mst_coa b on
		a.coa_debet_id = b.id	WHERE NO_VOUCHER = '".$nomor."' "; 
		
		$command = Yii::app()->db->createCommand($sql);
		$datas = $command->queryAll();
		$model = TrxVoucher::model()->find("trim(no_voucher)='".$nomor."'");
	
			
		$session=new CHttpSession;
            $session->open();
		Yii::import('application.extensions.giiplus.bootstrap.*');
		require_once('tcpdf/tcpdf.php');
		require_once('tcpdf/config/lang/eng.php');
		
		$html = $this->renderPartial('excelVoucher', array(
			'datas'=>$datas,
			'model'=>$model,
		), true);
		
			$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, array(220, 210), true, 'UTF-8', false);
			$pdf->SetCreator(PDF_CREATOR);
			$pdf->SetAutoPageBreak(TRUE, 0);
			$pdf->setPrintHeader(false);
			$pdf->setPrintFooter(false);
			$pdf->SetAutoPageBreak(TRUE, 0);
			$pdf->SetFont('dejavusans', '', 10);
			$pdf->AddPage();
			$pdf->writeHTML($html, true, true, true, true, '');
			$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
			$pdf->LastPage();
			
		$pdf->Output("trx_voucher.pdf", "I");
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}
	
	public function actionPosting()
	{	
		$no_voucher = $_REQUEST['no_voucher'];
		$sql = " UPDATE trx_voucher SET posting = 2 WHERE no_voucher = '".$no_voucher."' ";
		Yii::app()->db->createCommand($sql)->queryAll();

		$model=new TrxVoucher('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['TrxVoucher'])) {
			$model->attributes=$_GET['TrxVoucher'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
		
	}

	public function actionEdit()
	{	
		$no_voucher = $_REQUEST['nomor'];
		$sql = " SELECT * FROM trx_voucher  WHERE no_voucher = '".$no_voucher."' ";
		$data  = Yii::app()->db->createCommand($sql)->queryAll();
		
		$sql2 = " select a.mst_client_id,a.coa_credit_id,b.name from (
select mst_client_id,coa_credit_id from trx_voucher where no_voucher='".$no_voucher."' group by mst_client_id,coa_credit_id
)a inner join mst_client b on a.mst_client_id = b.id ";
		$data2 = Yii::app()->db->createCommand($sql2)->queryRow();

		$model=new TrxVoucher;
		$model3=new MstClient('searchAllnota');
		$model3->unsetAttributes();  
		if (isset($_GET['MstClient'])) {
			$model3->attributes=$_GET['MstClient'];
		}
		
		if (isset($_POST['TrxVoucher'])) {
			$sql3 = " DELETE FROM trx_voucher  WHERE no_voucher = '".$no_voucher."' ";
			
		$data3  = Yii::app()->db->createCommand($sql3)->queryAll();
		
			$model->attributes=$_POST['TrxVoucher'];
			$nota_note = $_POST['nota_note'];
			$client_name = $_POST['client_name'];
			$mst_client_id = $_POST['mst_client_id'];
			
			$coa_debet_id = $_POST['coa_debet_id'];
			$tgl_transaksi = $_POST['tgl_transaksi'];
			$jns_trx = $_POST['jns_trx'];
			$coa_debet_id = $_POST['coa_debet_id'];
			$jns_mata_uang = $_POST['jns_mata_uang'];
			$nominal = $_POST['nominal'];
			$keterangan = $_POST['keterangan'];
			//echo count($nota_note);exit;
			$nomor = "";
			for($i=0;$i<count($nota_note);$i++)
			{
				
				if($nota_note[$i]!=""){
					$model=new TrxVoucher;
					if($i != 0){
						$model->no_voucher = $nomor;
					}
					
					$model->client_name = $_POST['TrxVoucher']['client_name'];
					$model->mst_client_id = $_POST['TrxVoucher']['mst_client_id'];
					$model->coa_credit_id = $_POST['TrxVoucher']['coa_credit_id'];
					$model->tgl_transaksi = date('Y-m-d',strtotime($_POST['TrxVoucher']['tgl_transaksi']));
					//$model->jns_trx = $jns_trx[$i];
					$model->nota_note = $nota_note[$i];
					$model->coa_debet_id = $coa_debet_id[$i];
					$model->jns_mata_uang = $jns_mata_uang[$i];
					$model->nominal = trim($nominal[$i]);
					$model->keterangan = $keterangan[$i];
					$model->save();
						//echo "No Nota:".$nota_note[$i]."<br/>";
					//}else{
						//echo "Error";exit;
					//}
					if($i==0){
						$nomor = "VC-".date("Ymd").$model->id;
						TrxVoucher::model()->updateByPk($model->id,array('no_voucher'=>$nomor));
					}
					
				}
				
				
			}
		
				$this->redirect(array('admin'));
		}

		$this->render('edit',array(
			'model'=>$model,
			'data'=>$data,
			'model3'=>$model3,
			'data2'=>$data2,
		));
		
	}
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	 
	 function actionShowListnota(){
		$sql = " select x.* from (
select A.ID,'Premi Health' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NETT_TO_INSURANCE as NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NETT_TO_INSURANCE * C.NOMINAL AS FINAL_NOMINAL,'24' as ID_INSURANCE,b.mst_client_id
from ndk_health a
inner join reg_health_policy b on a.reg_health_policy_id = b.id
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
union
select A.ID,'P Non Health' AS JNS_INSURANCE,NOTA_DEBET_NO
,(a.premium + a.tpl - a.diskon_asuransi + a.policy_cost + a.stampduty_asuransi - a.brokerage - a.ppn + a.pph) as NOMINAL
,D.CODE,C.NOMINAL AS NOMINAL_CURR
,(a.premium + a.tpl - a.diskon_asuransi + a.policy_cost + a.stampduty_asuransi - a.brokerage - a.ppn + a.pph) * C.NOMINAL AS FINAL_NOMINAL
,no_type as ID_INSURANCE,b.mst_client_id
from ndk_health_non a
inner join reg_health_policy_non b on a.reg_nonhealth_policy_id = b.id
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
LEFT JOIN MST_JNS_ASURANSI E ON B.mst_jns_asuransi_id = E.id
union
SELECT a.id,'GK Health' as JNS_INSURANCE, a.NOTA_NO as NOTA_DEBET_NO, a.NETT_TO_INSURANCE as NOMINAL,'Rp' as CODE,  1 as NOMINAL_CURR,a.NETT_TO_INSURANCE * 1 as FINAL_NOMINAL,'1111' as ID_INSURANCE,b.mst_client_id FROM ganti_kartu a 
inner join reg_health_policy b on a.reg_policy_id = b.id
where a.type_reg_policy = '1'
union
SELECT a.id,'GK Health' as JNS_INSURANCE, a.NOTA_NO as NOTA_DEBET_NO,
 a.NOMINAL,'Rp' as CODE,  1 as NOMINAL_CURR,a.NOMINAL * 1 as FINAL_NOMINAL,'1111' as ID_INSURANCE,
 b.mst_client_id FROM ganti_kartu a 
inner join reg_health_policy_non b on a.reg_policy_id = b.id
where a.type_reg_policy = '2'
union
select A.ID,'NK Health' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NETT_TO_INSURANCE as NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NETT_TO_INSURANCE * C.NOMINAL AS FINAL_NOMINAL,'24' as ID_INSURANCE,b.mst_client_id
from ndk_health_kredit a
inner join reg_health_policy b on a.reg_health_policy_id = b.id
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
union
select A.ID,'NK Non Health' AS JNS_INSURANCE,NOTA_DEBET_NO,A.NETT_TO_INSURANCE as NOMINAL,D.CODE,C.NOMINAL AS NOMINAL_CURR,A.NETT_TO_INSURANCE * C.NOMINAL AS FINAL_NOMINAL,no_type as ID_INSURANCE,b.mst_client_id
from ndk_health_non_kredit a
inner join reg_health_policy_non b on a.reg_nonhealth_policy_id = b.id
INNER JOIN MST_CURRENCY_DETAIL C ON B.MST_CURRENCY_DETAIL_ID  = C.ID
INNER JOIN MST_CURRENCY D ON C.CURRENCY_ID = D.ID
LEFT JOIN MST_JNS_ASURANSI E ON B.mst_jns_asuransi_id = E.id
)x left join trx_receipt y on x.nota_debet_no = y.nota_note WHERE y.nota_note is not null  ";

		$command = Yii::app()->db->createCommand($sql);
		$datas = $command->queryAll();
		if(count($datas)>=1){
			echo json_encode($datas);
		}else{
			echo json_encode(array("id"=>""));
		}
			Yii::app()->end();
	}

	
	public function actionCreate()
	{
		$model=new TrxVoucher;
		$model3 = MstPerusahaanAsuransi::model()->findAll();
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['TrxVoucher'])) {
			$model->attributes=$_POST['TrxVoucher'];
			$nota_note = $_POST['nota_note'];
			$client_name = $_POST['client_name'];
			$mst_client_id = $_POST['mst_client_id'];
			
			$coa_debet_id = $_POST['coa_debet_id'];
			$tgl_transaksi = $_POST['tgl_transaksi'];
			$jns_trx = $_POST['jns_trx'];
			$coa_debet_id = $_POST['coa_debet_id'];
			$jns_mata_uang = $_POST['jns_mata_uang'];
			$nominal = $_POST['nominal'];
			$keterangan = $_POST['keterangan'];
			//echo count($nota_note);exit;
			$nomor = "";
			for($i=0;$i<count($nota_note);$i++)
			{
				
				if($nota_note[$i]!=""){
					$model=new TrxVoucher;
					if($i != 0){
						$model->no_voucher = $nomor;
					}
					$model->client_name = $_POST['TrxVoucher']['client_name'];
					$model->mst_client_id = $_POST['TrxVoucher']['mst_client_id'];
					$model->coa_credit_id = $_POST['TrxVoucher']['coa_credit_id'];
					$model->tgl_transaksi = date('Y-m-d',strtotime($_POST['TrxVoucher']['tgl_transaksi']));
					//$model->jns_trx = $jns_trx[$i];
					$model->nota_note = $nota_note[$i];
					$model->coa_debet_id = $coa_debet_id[$i];
					$model->jns_mata_uang = $jns_mata_uang[$i];
					$model->nominal = trim($nominal[$i]);
					$model->keterangan = $keterangan[$i];
					$model->save();
						//echo "No Nota:".$nota_note[$i]."<br/>";
					//}else{
						//echo "Error";exit;
					//}
					if($i==0){
						if($_POST['TrxVoucher']['coa_credit_id'] == '10'){
							$nomor = "PC-".date("Ymd").$model->id;
						}else{
							$nomor = "VC-".date("Ymd").$model->id;
						}
						TrxVoucher::model()->updateByPk($model->id,array('no_voucher'=>$nomor));
					}
					//echo $_POST['TrxVoucher']['client_name']."-".$_POST['TrxVoucher']['mst_client_id']."-".$_POST['TrxVoucher']['coa_credit_id']."-".date('Y-m-d',strtotime($_POST['TrxVoucher']['tgl_transaksi']))."-".$nota_note[$i]."-".$coa_debet_id[$i]."-".$jns_mata_uang[$i]."-". $nominal[$i]."-".$keterangan[$i]."=====";
				}
				
				
			}
		
				$this->redirect(array('admin'));
		}

		$this->render('create',array(
			'model'=>$model,
			'model3'=>$model3,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['TrxVoucher'])) {
			$model->attributes=$_POST['TrxVoucher'];
			if ($model->save()) {
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if (Yii::app()->request->isPostRequest) {
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if (!isset($_GET['ajax'])) {
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
			}
		} else {
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('TrxVoucher');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new TrxVoucher('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['TrxVoucher'])) {
			$model->attributes=$_GET['TrxVoucher'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return TrxVoucher the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=TrxVoucher::model()->findByPk($id);
		if ($model===null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param TrxVoucher $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if (isset($_POST['ajax']) && $_POST['ajax']==='trx-voucher-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}